﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using PropertyChanged;
using ZXing;
using ZXing.Common;
using ZXing.QrCode;

namespace PrintDll
{
    /// <summary>
    /// ChiFengTag.xaml 的交互逻辑
    /// </summary>
    [AddINotifyPropertyChangedInterface]
    public partial class ChiFengTag : UserControl
    {
        /// <summary>
        /// 货物名称
        /// </summary>
        public string GoodsName { get; set; }
        /// <summary>
        /// 当前时间
        /// </summary>
        public string Datetime { get; set; }

        public ImageSource QrCode { get; set; }
        /// <summary>
        /// 车牌号
        /// </summary>
        public string CarNumber { get; set; }
        /// <summary>
        /// 发货单位
        /// </summary>
        public string SendCpt { get; set; }

        /// <summary>
        /// 收货单位
        /// </summary>
        public string RevCpt { get; set; }
        /// <summary>
        /// 毛重
        /// </summary>

        public string Gross { get; set; }

        /// <summary>
        /// 皮重
        /// </summary>
        public string Tare { get; set; }
        /// <summary>
        /// 净重
        /// </summary>
        public string Net { get; set; }
        /// <summary>
        /// 司磅员
        /// </summary>
        public string UserName { get; set; }
        public ChiFengTag(string carNumber,string sendCpt, string revCpt, string gross, string tare, string net,string qrContent,string goodsName,string username,DateTime? time = null)
        {
            InitializeComponent();
            SendCpt = sendCpt;
            RevCpt = revCpt;
            Gross = gross;
            Tare = tare;
            Net = net;
            if (time != null) Datetime = time?.ToString("yyyy/MM/dd HH:mm:ss");
            else Datetime = DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss");
            QrCode = createQRCode(qrContent,50,50);
            GoodsName = goodsName;
            CarNumber = carNumber;
            UserName = username;
        }

        /// <summary>
        /// 创建二维码图片
        /// </summary>
        /// <param name="content"></param>
        /// <param name="width"></param>
        /// <param name="height"></param>
        /// <returns></returns>
        private ImageSource createQRCode(string content, int width, int height)
        {
            EncodingOptions options;
            //包含一些编码、大小等的设置
            //BarcodeWriter :一个智能类来编码一些内容的条形码图像
            BarcodeWriter write = null;
            options = new QrCodeEncodingOptions
            {
                DisableECI = true,
                CharacterSet = "UTF-8",
                Width = width,
                Height = height,
                Margin = 0
            };
            write = new BarcodeWriter();
            //设置条形码格式
            write.Format = BarcodeFormat.QR_CODE;
            //获取或设置选项容器的编码和渲染过程。
            write.Options = options;
            //对指定的内容进行编码，并返回该条码的呈现实例。渲染属性渲染实例使用，必须设置方法调用之前。
            Bitmap bitmap = write.Write(content);
            IntPtr bmpHandle = bitmap.GetHbitmap();
            //从GDI+ Bitmap创建GDI位图对象
            //Imaging.CreateBitmapSourceFromHBitmap方法，基于所提供的非托管位图和调色板信息的指针，返回一个托管的BitmapSource
            BitmapSource bitmapSource = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(bmpHandle, IntPtr.Zero, Int32Rect.Empty,
                BitmapSizeOptions.FromEmptyOptions());

            return bitmapSource;
        }
    }
}
